<template>
    <div class="flex flex-col items-center justify-center h-screen">
        <div class="bg-white h-auto rounded-2xl shadow-lg p-10 md:w-1/3 sm:w-1/2 lg:w-1/3 xl:w-1/5  animate__animated animate__fadeIn wow"
            data-wow-duration=".1s" data-wow-delay="0.5s">
            <div class="flex flex-col items-center justify-center">
                <div class="text-2xl font-bold  ">登录</div>
                <div class="text-sm text-gray-500">欢迎登录</div>
            </div>

            <div class="mt-10 mx-auto">
                <a-form :model="formData" :rules="rules" layout="vertical" @finish="onFinish" ref="formRef">
                    <a-form-item label="用户名" name="username">
                        <a-input allowClear v-model:value="formData.username" />
                    </a-form-item>

                    <a-form-item label="密码" name="password">
                        <a-input-password allowClear v-model:value="formData.password" />
                    </a-form-item>

                    <div class="flex items-center justify-between">
                        <a-form-item name="remember">
                            <a-checkbox v-model:checked="formData.remember">30天免登录</a-checkbox>
                        </a-form-item>
                        <a-form-item name="remember">
                            <a-checkbox v-model:checked="formData.rememberMe">记住账号</a-checkbox>
                        </a-form-item>

                    </div>

                    <a-form-item>
                        <a-row :gutter="16">
                            <a-col :span="12">
                                <a-button type="primary" html-type="submit" class="w-full">登录</a-button>
                            </a-col>
                            <a-col :span="12">
                                <a-button type="primary" danger ghost class="w-full" @click="resetForm">重置</a-button>

                            </a-col>
                        </a-row>
                    </a-form-item>

                </a-form>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useUserStore } from '@/utils/stores/modules/user'
import { ACCOUNT_KEY } from '@/utils/enums/cacheEnums'
import cache from '@/utils/cache';
import { useRoute, useRouter } from 'vue-router';
import { PageEnum } from '@/utils/enums/pageEnums';

const route = useRoute()
const router = useRouter()


const userStore = useUserStore()
const formRef = ref<any>()

interface RootName{
  username: string;
  password: string;
  remember?: boolean;
  rememberMe?: boolean;
}
const formData = reactive<RootName>({
    username: '',
    password: '',
    remember: false,
    rememberMe: false,
})

const rules = reactive({
    username: [
        { required: true, message: '请输入用户名' },
        { min: 3, max: 10, message: '用户名长度在 3 到 10 个字符' },
    ],
    password: [
        { required: true, message: '请输入密码' },
        { min: 5, max: 16, message: '密码长度在 6 到 16 个字符' },
    ],
})

// 登录
const onFinish = async (values: any) => {
   
    cache.set(ACCOUNT_KEY, {
        remember: values.remember,
        username: values.username,
    })

    await userStore.login(values)

    const { query : { redirect } } =    route
    const path = typeof redirect === 'string' ? redirect : PageEnum.HOME
    router.push(path)
};

// 重置
const resetForm = () => {
    formRef.value.resetFields()
}
</script>
